﻿#define _CRT_SECURE_NO_WARNINGS
#include"mo.h"
void set_pos(short x, short y)
{
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x, y };//光标的位置
	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(houtput, pos);
}
void youxijiemian()
{
	set_pos(0, 0);
	for (int i = 0; i < 29; i++)
	{
		wprintf(L"%c",WALL);
	} 
	for (int i = 1; i < 26; i++)
	{
		set_pos(56, i);
		wprintf(L"%c", WALL);
	}
	for (int i = 1; i < 26; i++)
	{
		set_pos(0, i);
		wprintf(L"%c", WALL);
	}
	set_pos(0, 26);
	for (int i = 0; i < 29; i++)
	{
		wprintf(L"%c", WALL);
	}
	
}
void jiemian()
{
	set_pos(40, 14);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	set_pos(40, 20);
	system("pause");
	system("cls");//清屏
	set_pos(20, 15);
	printf("用 ↑ . ↓ . ← . → 分别控制蛇的移动， F3为加速，F4为减速\n");
	set_pos(40, 20);
	system("pause");
	system("cls");
	youxijiemian();//地图
}
void initgshe(pyouxi p)
{
	sesheng arr=NULL;
	for (int i = 0; i < 5; i++)
	{
		arr = (sesheng)malloc(sizeof(sedian));
		if (arr == NULL)
		{
			perror("initgshe() arr");
			return;
		}
		arr->poo = NULL;
		arr->x = pou_x+2*i;
		arr->y = pou_y;
		if (p->shetou == NULL)
		{
			p->shetou = arr;
		}
		else
		{
			arr->poo = p->shetou;
			p->shetou = arr;
		}
	}
	arr = p->shetou;//感觉影响不大
	while (arr)
	{
		set_pos(arr->x, arr->y);
		wprintf(L"%lc", BOOY);
		arr = arr->poo;
	}
	p->fan = you;
	p->swfen = 10;
	p->zfen = 0;
	p->shudu = 200;
	p->tai = ok;
}
void initsw(pyouxi p)
{
	int x = 0;
	int y = 0;
	//x是2的倍数
	// x 2-54
	// y 1-25
	chong:
	do {
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0 );
	sesheng arr = p->shetou;
	while (arr)
	{
		if (x == arr->x && y == arr->y)
		{
			goto chong;
		}
		arr = arr->poo;
	}
	sesheng ass = (sesheng)malloc(sizeof(sedian));
	if (ass == NULL)
	{
		perror("initsw() ass");
		return;
	}
	ass->x = x;
	ass->y = y;
	ass->poo = NULL;
	set_pos(x, y);
	wprintf(L"%lc", FOOD);
	p->shiwu = ass;
}

void init(pyouxi p)
{
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo = { 0 };
	GetConsoleCursorInfo(houtput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(houtput, &CursorInfo); //改变控制台光标信息
	//设置界面
	jiemian();
	//创蛇
	initgshe(p);
	initsw(p);
	
}
void bangzhu()
{
	set_pos(64, 15);
	printf("不能穿墙，不能咬到自己\n");
	set_pos(64, 16);
	printf("用↑.↓.←.→分别控制蛇的移动.");
	set_pos(64, 17);
	printf("F3 为加速，F4 为减速\n");
	set_pos(64, 18);
	printf("ESC ：退出游戏.space：暂停游戏.");
}
#define KEY(vk) ((GetAsyncKeyState(vk)&0x1) ? 1 : 0)
void ting()
{
	while (1)
	{
		Sleep(200);
		if (KEY(VK_SPACE))
		{
			break;
		}
	}
}
int xiadian(sesheng arr, pyouxi p)
{
	return (arr->x == p->shiwu->x && arr->y == p->shiwu->y);
}
void yessw(sesheng arr, pyouxi p)
{
	arr->poo = p->shetou;
	p->shetou = arr;
	sesheng ass = p->shetou;
	while (ass)
	{
		set_pos(ass->x, ass->y);
		wprintf(L"%lc", BOOY);
		ass = ass->poo;
	}
	p->zfen += p->swfen;
	free(p->shiwu);
	initsw(p);
}
void noshiwu(sesheng arr, pyouxi p)
{
	arr->poo = p->shetou;
	p->shetou = arr;
	sesheng ass = p->shetou;
	while (ass->poo->poo)
	{
		set_pos(ass->x, ass->y);
		wprintf(L"%lc", BOOY);
		ass = ass->poo;
	}
	set_pos(ass->poo->x, ass->poo->y);
	printf("  ");
	free(ass->poo);
	ass->poo = NULL;
}
void panqiang(pyouxi p)
{
	if (p->shetou->x == 0 || p->shetou->x == 56 || p->shetou->y == 0 || p->shetou->y == 26)
	{
		p->tai = zqiang;
	}
}
void panzj(pyouxi p)
{
	sesheng arr = p->shetou->poo;
	while (arr)
	{
		if (arr->x == p->shetou->x && arr->y == p->shetou->y)
		{
			p->tai = zwo;
			break;
		}
		arr = arr->poo;
	}
}
void shezou(pyouxi p)
{
	sesheng arr= (sesheng)malloc(sizeof(sedian));
	if (arr == NULL)
	{
		perror("shezou() arr");
		return;
	}
	switch(p->fan)
	{
	  case shang:
		  arr->x = p->shetou->x;arr->y = p->shetou->y-1;
		  break;
	  case xia:
		  arr->x = p->shetou->x; arr->y = p->shetou->y + 1;
		  break;
	  case zhuo:
		  arr->x = p->shetou->x-2; arr->y = p->shetou->y;
		  break;
	  case you:
		  arr->x = p->shetou->x+2; arr->y = p->shetou->y;
		  break;
	}
	if (xiadian(arr,p))
	{
		yessw(arr,p);
	}
	else
	{
		noshiwu(arr,p);
	}
	panqiang(p);
	panzj(p);
}
void jie(pyouxi p)
{
	set_pos(29, 15);
	switch (p->tai)
	{
	case zqiang:printf("你已撞墙，游戏结束了\n"); break;
	case zwo:printf("你已撞到自己，游戏结束了\n"); break;
	case tui:printf("你已正常退出游戏\n"); break;
	}
	sesheng arr = p->shetou;
	while (arr)
	{
		sesheng ass = arr;
		arr = arr->poo;
		free(ass);
	}
}
void yunxing(pyouxi p)
{
	bangzhu();
	do
	{
		set_pos(64, 9);
		printf("速度%3d\n", p->shudu);
		set_pos(64, 10);
		printf("总分数%d \n", p->zfen);
		set_pos(64, 11);
		printf("当前食物分数%2d\n", p->swfen);
		if (KEY(VK_UP) && p->fan != xia)
		{
			p->fan = shang;
		}
		else if (KEY(VK_DOWN) && p->fan != shang)
		{
			p->fan = xia;
		}
		else if (KEY(VK_LEFT) && p->fan != you)
		{
			p->fan = zhuo;
		}
		else if (KEY(VK_RIGHT) && p->fan != zhuo)
		{
			p->fan = you;
		}
		else if (KEY(VK_SPACE))
		{
			ting();
		}
		else if (KEY(VK_ESCAPE))
		{
			p->tai = tui;
		}
		else if (KEY(VK_F3))
		{
			if (p->shudu >80)
			{
				p->shudu -= 30;
				p->swfen += 2;
			}
		}
		else if (KEY(VK_F4))
		{
			if (p->swfen>2)
			{
				p->shudu += 30;
				p->swfen -= 2;
			}
		}

		shezou(p);
		Sleep(p->shudu);
	} while (p->tai == ok);
	jie(p);
	
}